package com.blogspot.sunserenity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.blogspot.sunserenity.binarytools.BinaryTools;
import com.blogspot.sunserenity.blockcipher.DES.DESAlgorithm;

public class DESExampleActivity extends Activity {
	private TextView tvMessage;
	private TextView tvKey;
	private TextView tvCypher;
	private TextView tvDecrypted;
	private TextView tvPhone;
	private Button btEncrypt;
	private Button btDecrypt;
	private Button btSendSMS;
	
	//private DESAlgorithm des;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tvKey = (TextView)findViewById(R.id.key);
        tvMessage = (TextView)findViewById(R.id.message);
        tvCypher = (TextView)findViewById(R.id.cypher);
        tvDecrypted = (TextView)findViewById(R.id.decrypted);
        tvPhone = (TextView)findViewById(R.id.phonenumber);
        
        btEncrypt = (Button)findViewById(R.id.encrypt);
        btDecrypt = (Button)findViewById(R.id.decrypt);
        btSendSMS = (Button)findViewById(R.id.sendsms);
        
        btEncrypt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String	key = tvKey.getText().toString(),
						msg = tvMessage.getText().toString();
				if (validateKey (key) && msg.length() > 0) {
					DESAlgorithm des = new DESAlgorithm(BinaryTools.unicodeStringToBite(key, BinaryTools.DefaultCharCodeLen));
					byte[] cypher = des.encode(BinaryTools.unicodeStringToBite(msg, BinaryTools.DefaultCharCodeLen));
					String cypherStr = BinaryTools.unicodeCPtoString(BinaryTools.biteToUnicode(cypher, BinaryTools.DefaultCharCodeLen));
					tvCypher.setText(cypherStr);
				}
			}
		});
        btDecrypt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String	key = tvKey.getText().toString(),
						cypherStr = tvCypher.getText().toString();
				if (validateKey (key) && cypherStr.length() > 0) {
					DESAlgorithm des = new DESAlgorithm(BinaryTools.unicodeStringToBite(key, BinaryTools.DefaultCharCodeLen));
					byte[] cypher = BinaryTools.unicodeStringToBite(cypherStr, BinaryTools.DefaultCharCodeLen);
					tvDecrypted.setText(
									BinaryTools.unicodeCPtoString(
											BinaryTools.biteToUnicode(
													des.decode(cypher), 
													BinaryTools.DefaultCharCodeLen
													)));
				}
			}
		});      
        btSendSMS.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String	cypher = tvCypher.getText().toString(),
						phone = tvPhone.getText().toString();			
				if (cypher.length() > 0 && phone.length() > 0) {
					sendSms (cypher, phone);
				}
			}
		});
    }
    private boolean validateKey (String key) {
    	boolean isValid = true;
    	if (key.length() != 8) {
			Toast.makeText(getApplicationContext(), "Key must be 8 ASCII characters!", 5000).show();
			isValid = false;
		}
    	return isValid;
    }
    // Not a good idea but it's working for now
    @SuppressWarnings("deprecation")
	private void sendSms (String msg, String phone) {
    	PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Object.class), 0);
		SmsManager sms = SmsManager.getDefault();
    	sms.sendTextMessage(phone, null, msg, pi, null);
    	Toast.makeText(this, "Ciphered message was sended.", 5000).show();
    }
}